﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;
using System.Text;

namespace WebSite.admin.warehouse
{
    public partial class warehouseGoodsInventoryList : BaseAdmin
    {

        readonly
        tbCardType oArea;
        readonly DALWarehouseGoodsInventory theWarehouseGoodsInventory = new DALWarehouseGoodsInventory();
        readonly DALWarehouseGoodsInventoryDetail theWarehouseGoodsInventoryDetail = new DALWarehouseGoodsInventoryDetail();

        tbWarehouseGoodsInventory oWarehouseGoodsInventory;

        // Methods
        private void BindGrid()
        {
            string sWhere = " warehouseId in (select id from tbWarehouse where  warehouseName like '%" + this.txtKeywords.Text.Trim() + "%')";

            DataTable table = theWarehouseGoodsInventory.GetList(sWhere).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };
        }





        protected void btnBrachDeleted_Click1(object sender, EventArgs e)
        {

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sWarehouse = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sWarehouse.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sWarehouse.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sWarehouse.Length > 0)
            {
                theWarehouseGoodsInventoryDetail.DeleteListInventoryIds(sWarehouse.ToString());
                this.theWarehouseGoodsInventory.DeleteList(sWarehouse.ToString());
            }
            ExtAspNet.Alert.Show("批量删除盘点成功！");
            this.BindGrid();
        }






        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.WarehouseGoodsInventoryManager);

                this.DeleteItem();
                this.SubmitItem();
                this.BindGrid();

            }
        }

        void DeleteItem()
        {
            if (Request.QueryString["action"] == "del" && Request.QueryString["id"] != null)
            {
                this.theWarehouseGoodsInventoryDetail.DeleteByInventoryId(int.Parse(Request.QueryString["id"]));
                this.theWarehouseGoodsInventory.Delete(int.Parse(Request.QueryString["id"]));
                ExtAspNet.Alert.Show("删除盘点成功！");
            }

        }

        void SubmitItem()
        {
            if (Request.QueryString["action"] == "sub" && Request.QueryString["id"] != null)
            {
                oWarehouseGoodsInventory = theWarehouseGoodsInventory.GetModel(int.Parse(Request.QueryString["id"]));
                if (oWarehouseGoodsInventory != null)
                {
                    oWarehouseGoodsInventory.inventoryStates = 1;
                    if (theWarehouseGoodsInventory.Update(oWarehouseGoodsInventory))
                    {
                        ExtAspNet.Alert.Show("提交成功！");
                    }
                    else
                    {
                        ExtAspNet.Alert.Show("提交失败！");
                    }

                }
            }
        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            if (Grid1.Rows[e.RowIndex].DataKeys[1].ToString() == "0")
            {
                Grid1.Rows[e.RowIndex].FindControl("divEdit").Visible = true;
                Grid1.Rows[e.RowIndex].FindControl("divDeleted").Visible = true;
                Grid1.Rows[e.RowIndex].FindControl("divSubmit").Visible = true;
            }
            else
            {
                Grid1.Rows[e.RowIndex].FindControl("divEdit").Visible = false;
                Grid1.Rows[e.RowIndex].FindControl("divDeleted").Visible = false;
                Grid1.Rows[e.RowIndex].FindControl("divSubmit").Visible = false;
 
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            string sWhere = " warehouseId in (select id from tbWarehouse where  warehouseName like '%" + this.txtKeywords.Text.Trim() + "%')";

            DataTable table = theWarehouseGoodsInventory.GetList(sWhere).Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        protected void btnAddInventory_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("warehouseGoodsInventory.aspx?ot=ad");
        }

    }
}